import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/collect/api.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/utils/index.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class AddCollect extends StatefulWidget {
  const AddCollect({super.key});

  @override
  State<StatefulWidget> createState() => _AddCollectState();

}

class _AddCollectState extends State<AddCollect> {
  final Map _params = {
    "name": "",
    "number": "",
    "description": ""
  };

  bool _canSubmit() {
    return Utils.isNotEmpty(_params["name"]) && Utils.isNotEmpty(_params["number"]);
  }

  Future<void> _submitAddCollect() async {
    try {
      Map resData = await collectApi.saveCollect(_params);
      if (resData["code"] == 200) {
        EasyLoading.showToast("添加成功");
        Stacker.pop(true);
      }
    } catch (error) {
      print(error);
    }
  }


  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "添加采集器",
      child: ListView(
        padding: const EdgeInsets.all(ThemeVars.paddingPage),
        children: [
          MenuBlock(
            items: [
              MenuBlockItem(
                text: "采集器名称",
                showArrow: false,
                suffix: TextField(
                  textAlign: TextAlign.right,
                  decoration: const InputDecoration(
                    hintText: "请输入",
                    hintStyle: TextStyle(color: ThemeVars.colorTextTer, fontWeight: FontWeight.w400, fontSize: 15),
                    isDense: true
                  ),
                  onChanged: (value) => setState(() {
                    _params["name"] = value;
                  }),
                )
              ),
              MenuBlockItem(
                text: "采集器编号",
                showArrow: false,
                suffix: TextField(
                  textAlign: TextAlign.right,
                  decoration: const InputDecoration(
                      hintText: "请输入",
                      hintStyle: TextStyle(color: ThemeVars.colorTextTer, fontWeight: FontWeight.w400, fontSize: 15),
                      isDense: true
                  ),
                  onChanged: (value) => setState(() {
                    _params["number"] = value;
                  }),
                )
              ),
              MenuBlockItem(
                text: "备注",
                showArrow: false,
                suffix: TextField(
                  textAlign: TextAlign.right,
                  decoration: const InputDecoration(
                    hintText: "请输入",
                    hintStyle: TextStyle(color: ThemeVars.colorTextTer, fontWeight: FontWeight.w400, fontSize: 15),
                    isDense: true
                  ),
                  onChanged: (value) => setState(() {
                    _params["description"] = value;
                  }),
                )
              ),
            ]
          ),
          Padding(
            padding: const EdgeInsets.only(top: 40),
            child: Button(
              type: ButtonType.primary,
              onPressed: _canSubmit() ? () async {
                await _submitAddCollect();
              } : null,
              child: const Text("确定"),
            ),
          )
        ],
      ),
    );
  }

}